package com.bobo.common.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;

/**
 * @Description: 通用响应数据
 * @Auther: Swain
 * @Date: 2021/1/8 15:08
 * @Version 1.0.0
 */
@Data
@ApiModel
public class ServerResponse<T> implements Serializable {

    private static final long serialVersionUID = 44321346635165465L;
    @ApiModelProperty("响应码")
    private int resultCode;
    @ApiModelProperty("响应信息")
    private String resultMsg;
    @ApiModelProperty("响应数据")
    private T resultObj;
    @ApiModelProperty("时间戳")
    private Date timestamp;

    public static <T> ServerResponse<T> createBySuccess() {
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setResultCode(ServerCode.SUCCESS.getCode());
        serverResponse.setResultMsg(ServerCode.SUCCESS.getDesc());
        serverResponse.setTimestamp(new Date());
        return serverResponse;
    }

    public static <T> ServerResponse<T> createBySuccess(T resultObj) {
        ServerResponse<T> serverResponse = ServerResponse.createBySuccess();
        serverResponse.setResultObj(resultObj);
        return serverResponse;
    }

    public static <T> ServerResponse<T> createByError() {
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setResultCode(ServerCode.SYSTEM_ERROR.getCode());
        serverResponse.setResultMsg(ServerCode.SYSTEM_ERROR.getDesc());
        serverResponse.setTimestamp(new Date());
        return serverResponse;
    }

    public static <T> ServerResponse<T> createByErrorMessage(int errorCode, String errorMessage) {
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setResultCode(errorCode);
        serverResponse.setResultMsg(errorMessage);
        serverResponse.setTimestamp(new Date());
        return serverResponse;
    }

    public static <T> ServerResponse<T> createByErrorCodeMessage(ServerCode serverCode) {
        ServerResponse<T> serverResponse = new ServerResponse<>();
        serverResponse.setResultCode(serverCode.getCode());
        serverResponse.setResultMsg(serverCode.getDesc());
        serverResponse.setTimestamp(new Date());
        return serverResponse;
    }

}
